package ChatLib;

import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JOptionPane;

import org.jgroups.Address;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.Receiver;
import org.jgroups.View;
import org.jgroups.protocols.UDP;

public final class TransportDriver extends JChannel implements Receiver{
	
	String clusterName;
	TransportReceiver transportReceiver;

	public TransportDriver(final String clusterName, TransportReceiver transportReceiver) throws Exception {
		super();
		logger.debug("Forcing to IPv4 stack");
		System.setProperty("java.net.preferIPv4Stack", "true");
		System.setProperty("jgroups.bind_addr", "127.0.0.1");
		
		this.clusterName=clusterName;
		this.transportReceiver=transportReceiver;
		
		try{
			JChannel drvChannel= new JChannel();
			UDP protocol= (UDP)dvrChannel.getProtocolStack().getTransport();
			if(getPort(clusterName)!=0)
				protocol.setMulticastPort(getPort(clusterName));
			else
				JOptionPane.showMessageDialog(null, "can't find a port, default is used");
			dvrChannel.connect(clusterName);
			dvrChannel.setReceiver(this);
			dvrChannel.setDiscardOwnMessages(true);
		}
		catch(Exception e) {
			logger.error("<<class Abstract Channel Driver>>cannot create a channel");
		}
	}

	@Override
	public void getState(OutputStream arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void receive(Message arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setState(InputStream arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void block() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void suspect(Address arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unblock() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void viewAccepted(View arg0) {
		// TODO Auto-generated method stub
		
	}

}
